package com.kkshop.gwt.registration;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;
import com.kkshop.gwt.util.ConfigUtils;
import com.kkshop.gwt.widget.wizard.impl.AbstractWizard;

public class RegistrationWizard extends AbstractWizard {
    
    private static final String REGISTRATION_FAILURE_STATUS_CODE = "ERROR";
    private static final String REGISTRATION_SUCCESS_STATUS_CODE = "SUCCESS";
    
    private RegistrationModel model;
    
    public RegistrationWizard(RegistrationModel model) {
        this.model = model;
    }

    public void addPages() {
        addPage(new DisclaimerWizardPage());
        if(!ConfigUtils.isSingleEnvironment()) {
            addPage(new AccountTypeWizardPage());
        }
        addPage(new AccountInformationWizardPage());
    }

    public void cancel() {
    }

    public boolean finish() {
        FormPanel form = getForm();
        RootPanel.get().add(form);
        form.submit();
        return true;
    }
    
    private FormPanel getForm() {
        FormPanel form = new FormPanel();
        
        form.setMethod(FormPanel.METHOD_POST);
        
        if("CUSTOMER".equalsIgnoreCase(model.getRole())) {
            form.setAction("/registerCustomer.action");
        } else if("PARTNER".equalsIgnoreCase(model.getRole())) {
            form.setAction("/registerPartner.action");
        }
        
        form.addFormHandler(new  FormHandler() {
            public void onSubmit(FormSubmitEvent event) {
            }

            public void onSubmitComplete(FormSubmitCompleteEvent event) {
                Document doc = XMLParser.parse(event.getResults().trim());
                String code = getElementText(doc, "code");
                String result = getElementText(doc, "result");
                if(REGISTRATION_SUCCESS_STATUS_CODE.equalsIgnoreCase(code)) {
                    Window.alert(result);
                    Window.open("/main.action", "_self", null);
                } else if(REGISTRATION_FAILURE_STATUS_CODE.equalsIgnoreCase(code)) {
                    Window.alert(result);
                } else {
                    Window.alert("Sorry! Try again later...");
                }
            }
        });
        
        VerticalPanel panel = new VerticalPanel();
        panel.add(new Hidden("username", model.getUsername()));
        panel.add(new Hidden("password", model.getPassword()));
        form.setWidget(panel);
        
        form.setVisible(false);
        
        return form;
    }
    
    protected RegistrationModel getModel() {
        return model;
    }
    
    private String getElementText(Document document, String elementName) {
        return document.getDocumentElement().getElementsByTagName(elementName).item(0).getFirstChild().getNodeValue();
    }
}
